package com.easy.service.impl;

import com.easy.bean.Attraction;
import com.easy.bean.User;
import com.easy.mapper.AttractionDao;
import com.easy.mapper.UserDao;
import com.easy.service.AttractionService;
import com.easy.service.UserService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class AttractionServiceImpl implements AttractionService {

    private static final String CACHE_NAME = "attraction";

    @Autowired
    AttractionDao attractionDao;

    @Override
   @CacheEvict(value = CACHE_NAME, key = "'attraction:' + #p0.id", condition = "#result != null")
    public Attraction add(Attraction attraction) {
        //新增数据
        attractionDao.add(attraction);
        //通过主键查询数据库的数据
        Attraction result =attractionDao.one(attraction.getId());
        return result;
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "'attraction:id:' + #p0")
    public int del(Integer id) {
        return attractionDao.del(id);
    }

    @Override
   @CacheEvict(value = CACHE_NAME, key = "'attraction:' + #p0.id", condition = "#p0 != null and #p0.id != null")
    public Attraction edit(Attraction attraction) {
        attractionDao.edit(attraction);
        Attraction result = attractionDao.one(attraction.getId());
        return result;
    }

    @Override
  @Cacheable(value = CACHE_NAME, key = "'attraction:list:' + T(java.util.Objects).hash(#p0)", unless = "#result == null or #result.isEmpty()")
    public List<Attraction> all(Attraction attraction) {
        return attractionDao.all(attraction);
    }

    @Override
    @Cacheable(value = CACHE_NAME, key = "'attraction:id:' + #p0", unless = "#result == null")
    public Attraction one(Integer id) {
        return attractionDao.one(id);
    }

    @Override
    public void page(Page page, Attraction attraction) {
        int count = attractionDao.pageCount(attraction);
        Object data = attractionDao.page(page, attraction);
        page.setCount(count);
        page.setData(data);
    }
    // 新增方法实现：根据城市和偏好获取景点
    @Override
    public List<Attraction> getByCityAndPreferences(String city, String preferences) {
        Attraction query = new Attraction();
        query.setCity(city);

        // 获取所有该城市的景点
        List<Attraction> cityAttractions = attractionDao.all(query);

        // 如果没有偏好或者没有获取到景点，直接返回该城市的所有景点
        if (preferences == null || preferences.isEmpty() || cityAttractions.isEmpty()) {
            return cityAttractions;
        }

        // 根据偏好筛选景点
        List<String> preferenceList = Arrays.asList(preferences.split(","));
        return cityAttractions.stream()
                .filter(attraction -> attraction.getType() != null &&
                        preferenceList.stream().anyMatch(preference ->
                                attraction.getType().contains(preference.trim())))
                .collect(Collectors.toList());
    }
}
